<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/16
 * Time: 11:14
 */
namespace App\Http\Controllers\Api;

use App\Constant\ErrorConstant;
use App\Service\Api\NotifyService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class NotifyController extends BaseController
{
    protected $service;

    public function __construct(Request $request, NotifyService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 微信支付结果通知处理
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function payNotify()
    {
        $xml_data   =   $this->request->getContent();
        $file_path  =   'order_notify_' . Carbon::now()->format('Y_m_d') . '.txt';
        Storage::append($file_path, $xml_data);

        if (empty($xml_data)) {
            return response(xmlEncode([
                'return_code'  =>  'FAIL',
                'return_msg'   =>  '未接收到数据'
            ]),200)->header("Content-type","text/xml");
        } else {
            $notify_data    =   xmlObjectToArray(simplexml_load_string($xml_data, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS | LIBXML_NOERROR));
            [$flag, $result]=   $this->service->notifyPay($notify_data);
            if (!$flag) {
                return response(xmlEncode([
                    'return_code'  =>  'FAIL',
                    'return_msg'   =>  $result
                ]),200)->header("Content-type","text/xml");
            } else {
                return response(xmlEncode([
                    'return_code'  =>  'SUCCESS',
                    'return_msg'   =>  is_null($result) ? 'OK' : $result
                ]),200)->header("Content-type","text/xml");
            }
        }
    }

    /**
     * 会员开通处理结果
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function memberNotify()
    {
        $xml_data   =   $this->request->getContent();
        $file_path  =   'member_notify_' . Carbon::now()->format('Y_m_d') . '.txt';
        Storage::append($file_path, $xml_data);
        if (empty($xml_data)) {
            return response(xmlEncode([
                'return_code'  =>  'FAIL',
                'return_msg'   =>  '未接收到数据'
            ]),200)->header("Content-type","text/xml");
        } else {
            $notify_data    =   xmlObjectToArray(simplexml_load_string($xml_data, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS | LIBXML_NOERROR));
            [$flag, $result]=   $this->service->notifyMember($notify_data);
            if (!$flag) {
                return response(xmlEncode([
                    'return_code'  =>  'FAIL',
                    'return_msg'   =>  $result
                ]),200)->header("Content-type","text/xml");
            } else {
                return response(xmlEncode([
                    'return_code'  =>  'SUCCESS',
                    'return_msg'   =>  is_null($result) ? 'OK' : $result
                ]),200)->header("Content-type","text/xml");
            }
        }
    }

    /**
     * 微信订单查询
     * @return \Illuminate\Http\JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function queryNotify()
    {
        $validator  =   Validator::make($this->request->post(), [
            'order_number'  =>  'required|string'   //|exists:mini_order,order_no
        ], [
            'order_number.required' =>  '订单号不能为空',
            'order_number.string'   =>  '订单号必须为字符',
            'order_number.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_EXPRESS_ERROR);
        }

        $order_number   =   $this->request->post('order_number');

        [$flag, $result]=   $this->service->notifyQuery($order_number);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_EXPRESS_ERROR, $result);
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function expressNotify()
    {
        $validator  =   Validator::make($this->request->post(), [
            'express_no'  =>  'required|string'   //|exists:mini_order,order_no
        ], [
            'express_no.required' =>  '订单号不能为空',
            'express_no.string'   =>  '订单号必须为字符',
            'express_no.exists'   =>  '订单不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::API_AFTER_EXPRESS_ERROR);
        }

        $express_no   =   $this->request->post('express_no');

        [$flag, $result]=   $this->service->notifyExpress($express_no);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::API_AFTER_EXPRESS_ERROR, $result);
        }
    }

    /**
     * 阿里云直播回调通知地址
     */
    public function liveNotify()
    {
        $file_path  =   'aliyun_notify_live_' . Carbon::now()->format('Y_m_d') . '.txt';
        Storage::append($file_path, Carbon::now()->toDateTimeString() . ': 回调参数：' . json_encode($this->request->all(), JSON_UNESCAPED_UNICODE));
    }
}
